package com.SmartCare.service.impl.health;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.Result;
import com.SmartCare.service.health.BloodGlucoseService;
import com.SmartCare.common.vo.health.BloodGlucoseVo;
import com.SmartCare.common.vo.health.BloodGlucosesVo;
import com.SmartCare.mapper.health.BloodGlucoseMapper;
import com.SmartCare.pojo.health.BloodGlucose;
import com.SmartCare.utils.DateTimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BloodGlucoseServiceImpl extends ServiceImpl<BloodGlucoseMapper, BloodGlucose> implements BloodGlucoseService {
    /**
     * 查询一周血糖记录
     */
    @Override
    public Result bloodGlucoseQuery(Long elderlyId, String moment, String startTime, String endTime) {
        if(elderlyId==null) {
            return Result.error("未知错误");
        }

        //times :startTime 、endTime
        //LocalDateTime[] times = DateTimeUtils.getStartAndEndDateTime(6);
        //构造查询条件
        LambdaQueryWrapper<BloodGlucose> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodGlucose::getElderlyId,elderlyId);
        queryWrapper.between(BloodGlucose::getCreatedTime,startTime,endTime);
        queryWrapper.eq(BloodGlucose::getMoment,moment);
        queryWrapper.orderByAsc(BloodGlucose::getCreatedTime);

        //查询数据
        List<BloodGlucose> list = super.list(queryWrapper);
        if (list.isEmpty()) {
            return Result.error("无记录");
        }
        BloodGlucosesVo bloodGlucosesVo = new BloodGlucosesVo();
        List<BloodGlucoseVo> res = new ArrayList<>();

        //返回对象封装
        for (BloodGlucose item : list) {
            BloodGlucoseVo bloodGlucose = new BloodGlucoseVo();
            BeanUtils.copyProperties(item,bloodGlucose,"createdTime");
            bloodGlucose.setCreatedTime(DateTimeUtils.formatDateTime(item.getCreatedTime()));
            res.add(bloodGlucose);
        }
        bloodGlucosesVo.setBloodGlucoses(res);

        return Result.success(bloodGlucosesVo);
    }

    /**
     * 添加血糖记录
     */
    @Override
    public Result bloodGlucoseAdd(Long elderlyId, Float bloodGlucose, String moment) {
        if (elderlyId==null) {
            return Result.error("未知错误");
        }
        //查询记录，防止重复添加

        LambdaQueryWrapper<BloodGlucose> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodGlucose::getElderlyId,elderlyId);
        queryWrapper.eq(BloodGlucose::getMoment,moment);
        queryWrapper.like(BloodGlucose::getCreatedTime,DateTimeUtils.getCurrentDateTime());
        BloodGlucose one = getOne(queryWrapper);

        if (one==null){
            //记录不存在，正常添加
            BloodGlucose glucose = BloodGlucose.builder()
                    .elderlyId(elderlyId)
                    .bloodGlucose(bloodGlucose)
                    .moment(moment)
                    .build();
            boolean res = save(glucose);
            if (res){
                return Result.success("添加成功");
            }
            return Result.error("添加失败，请重新添加");
        }
        return Result.error("该记录已添加，请勿重复添加");
    }

}
